package com.cne.gmail.Gmail.websocket;


import com.cne.gmail.Gmail.model.AddressList;
import lombok.extern.log4j.Log4j2;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Controller;
import javax.annotation.Resource;
import java.util.List;


@Controller
@Log4j2
public class WsController {

    @Resource
    WebSocketService webSocketService;
    @MessageMapping("/welcome")//@MessageMapping和@RequestMapping功能类似，用于设置URL映射地址，浏览器向服务器发起请求，需要通过该地址。
    @SendTo("/topic/getResponse")//如果服务器接受到了消息，就会对订阅了@SendTo括号中的地址传送消息。
    public WiselyResponse say(WiselyMessage message) throws Exception {
        //webSocketService.send2User("zhangxuan000321@gmail.com",new WiselyResponse("你有新的未读消息"));
        return new WiselyResponse("Welcome, " + message.getName() + "!");
    }
    //一对一推送
    public void handleSubscribe(String userId, List<AddressList> addressLists, Object message) {
       if (message!=null){
           webSocketService.send2User(userId,new WiselyResponse("你有新的未读消息",addressLists,message) );
       }
    }
}